package com.geostar.constructionland.statistics.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.geostar.constructionland.common.entity.response.ExtJsAjaxResponse;
import com.geostar.constructionland.common.entity.statistics.UnifiedDataQueryConfig;
import com.geostar.constructionland.statistics.service.IDataQueryService;
import com.geostar.constructionland.statistics.service.IUnifiedDataQueryConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author makoto_zhu
 * @Date 2020-10-28 04:29:39
 * @Description 数据查询接口
 **/
@Api("数据查询控制器")
@RestController
@Slf4j
@Validated
public class DataQueryController {

    @Resource
    private IDataQueryService dataQueryService;

    @Resource
    private IUnifiedDataQueryConfigService unifiedDataQueryConfigService;


    @ApiOperation("根据查询id和查询参数执行查询")
    @PostMapping(path = "/statistics/query")
    public ExtJsAjaxResponse query(@NotBlank(message = "查询id不能为空") String uniqueQueryId, @NotBlank(message = "查询参数不能为空") String params) {
        List<Map<String, Object>> result;
        try {
            result = dataQueryService.queryData(uniqueQueryId, params);
            return ExtJsAjaxResponse.createBySuccess(result);
        } catch (Exception e) {
            return ExtJsAjaxResponse.createByErrorMessage(e.getMessage());
        }
    }

    @PostMapping(path = "/statistics/list")
    public ExtJsAjaxResponse uqcList(
            @NotNull(message = "页码不能为空") @RequestParam(value = "page") int pageNo,
            @NotNull(message = "每页数量不能为空") @RequestParam(value = "limit") int pageNum,
            String query_param_statistics_id) {
        IPage<UnifiedDataQueryConfig> dataQueryConfigIPage
                = unifiedDataQueryConfigService.getDataQueryConfigForPage(pageNo, pageNum, query_param_statistics_id);
        return ExtJsAjaxResponse.createBySuccess(dataQueryConfigIPage);
    }

    @ApiOperation("删除配置文件实体")
    @PostMapping(path = "/statistics/delete")
    public ExtJsAjaxResponse uqcDelete(@NotNull(message = "id不能为空") String id) {
        unifiedDataQueryConfigService.deleteById(id);
        return ExtJsAjaxResponse.createBySuccessMessage("删除配置文件成功");
    }

    @PostMapping(path = "/statistics/add")
    @ApiOperation("保存配置文件实体")
    public ExtJsAjaxResponse uqcAdd(@RequestBody UnifiedDataQueryConfig dataQueryConfig) {
        dataQueryConfig.setModifiedTime(new Date());
        boolean message = unifiedDataQueryConfigService.saveDataQueryConfig(dataQueryConfig);
        if (message) {
            return ExtJsAjaxResponse.createBySuccess(message);
        } else {
            return ExtJsAjaxResponse.createByErrorMessage("查询id重复");
        }
    }

    @PostMapping(path = "/statistics/update")
    @ApiOperation("保存配置文件实体")
    public ExtJsAjaxResponse uqcUpdate(@RequestBody UnifiedDataQueryConfig dataQueryConfig) {
        dataQueryConfig.setModifiedTime(new Date());
        boolean message = unifiedDataQueryConfigService.updateDataQueryConfig(dataQueryConfig);
        if (message) {
            return ExtJsAjaxResponse.createBySuccess(message);
        } else {
            return ExtJsAjaxResponse.createByErrorMessage("查询id重复");
        }
    }

}
